package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import beans.Bill;

public class BillDAO extends ConnBean {
	private Connection connection = null;
	
	
	/*
	 * 根据账单名查找账单
	 */
	public Bill getBill(int bno) {
		Bill bill = new Bill();
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = getConnection();
			if (connection == null)
				System.out.println("error");
			String sql = "select * from Bill where bno=" + bno;
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			if (resultSet.next()) {
				System.out.println("get it");
				bill.setBno(resultSet.getInt(1));
				bill.setType(resultSet.getInt(2));
				bill.setCost(resultSet.getLong(3));
				bill.setDetail(resultSet.getString(4));
			} else {
				System.out.println("null");
			}
			statement.close();
			connection.close();
			// return user;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (connection != null && (!connection.isClosed())) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return bill;
	}
	
	/*
	 * 插入账单 
	 */
	public int insertAccount(Bill bill) {
		PreparedStatement preparedStatement = null;
		String sql = "insert into Bill values(?,?,?,?)";
		int result = 0;
		if (bill== null)
			return result;
		int number = 0;
		try {
			connection = getConnection();
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1,bill.getBno() );
			preparedStatement.setInt(2,bill.getType());
			preparedStatement.setLong(3,bill.getCost());
			preparedStatement.setString(4, bill.getDetail());
			number = preparedStatement.executeUpdate();
			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (connection != null && (!connection.isClosed())) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		//System.out.println("***"+number);
		if (number > 0)
			result = 1;
		return result;
	}
	

}
